package hms.UserAccountsModule;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class UserAccountJList extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6538562590110631772L;

	private UserAccountControl allAccounts;
	private DefaultListModel listModel;
	private JList list;
	private JScrollPane scrollPane;

	public UserAccountJList() {
		super();
		allAccounts = new UserAccountControl();
		listModel = new DefaultListModel();
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(list);
		update();
		getContentPane().add(scrollPane);
	}

	public void update() {
		listModel.clear();
		listModel.ensureCapacity(allAccounts.size());
		for (UserAccount ua : allAccounts) {
			if (!ua.isMarkedForDeletion()) {
				listModel.addElement(ua);
			}
		}
		list.validate();
	}

	public UserAccount getSelectedUserAccount() {
		if (list.getSelectedIndex() == -1) {
			return null;
		} else {
			return (UserAccount) listModel.get(list.getSelectedIndex());
		}
	}

	public UserAccountControl getControl() {
		return allAccounts;
	}

	public static UserAccount showDialog() {
		UserAccountJList list = new UserAccountJList();
		Object[] options = { "OK", "Cancel" };

		JOptionPane.showOptionDialog(list, list.getContentPane(), "List Test",
				JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
				options, options[1]);

		return list.getSelectedUserAccount();
	}

	/*
	public static void main(String args[]) {

		UserAccount ua = UserAccountJList.showDialog();

		if (ua == null) {
			System.out.println("Nothing returned");
		} else {
			System.out.println(ua.toString());
		}
	}*/
}
